{% extends 'base/base.html' %}

{% block title %}
{% if recipe %}编辑做菜指南{% else %}创建做菜指南{% endif %}
{% endblock %}

{% block main %}
<div class="row justify-content-center">
    <div class="col-12">
        <div class="card">
            <div class="card-header bg-primary text-white">
                <h3 class="mb-0">
                    <i class="fas fa-utensils me-2"></i>
                    {% if recipe %}编辑做菜指南{% else %}创建做菜指南{% endif %}
                </h3>
            </div>
            <div class="card-body">
                <form method="post" enctype="multipart/form-data">
                    {% csrf_token %}

                    {% if form.errors or ingredient_formset.errors or step_formset.errors %}
                    <div class="alert alert-danger">
                        <i class="fas fa-exclamation-triangle me-2"></i>
                        请检查以下错误：
                        <ul class="mb-0 mt-2">
                            {% for field in form %}
                                {% for error in field.errors %}
                                    <li>{{ field.label }}: {{ error }}</li>
                                {% endfor %}
                            {% endfor %}
                            {% for error in ingredient_formset.errors %}
                                <li>食材信息: {{ error }}</li>
                            {% endfor %}
                            {% for error in step_formset.errors %}
                                <li>制作步骤: {{ error }}</li>
                            {% endfor %}
                        </ul>
                    </div>
                    {% endif %}

                    <!-- 基本信息 -->
                    <div class="card mb-4">
                        <div class="card-header">
                            <h5><i class="fas fa-info-circle me-2"></i>基本信息</h5>
                        </div>
                        <div class="card-body">
                            <div class="row">
                                <div class="col-md-6">
                                    <div class="mb-3">
                                        <label for="{{ form.name.id_for_label }}" class="form-label">菜品名称 *</label>
                                        {{ form.name }}
                                    </div>
                                </div>
                                <div class="col-md-3">
                                    <div class="mb-3">
                                        <label for="{{ form.category.id_for_label }}" class="form-label">菜品分类 *</label>
                                        {{ form.category }}
                                    </div>
                                </div>
                                <div class="col-md-3">
                                    <div class="mb-3">
                                        <label for="{{ form.servings.id_for_label }}" class="form-label">份数</label>
                                        {{ form.servings }}
                                    </div>
                                </div>
                            </div>
                            
                            <div class="row">
                                <div class="col-md-4">
                                    <div class="mb-3">
                                        <label for="{{ form.difficulty.id_for_label }}" class="form-label">制作难度</label>
                                        {{ form.difficulty }}
                                    </div>
                                </div>
                                <div class="col-md-4">
                                    <div class="mb-3">
                                        <label for="{{ form.cooking_time.id_for_label }}" class="form-label">制作时间</label>
                                        {{ form.cooking_time }}
                                    </div>
                                </div>
                                <div class="col-md-4">
                                    <div class="mb-3">
                                        <label for="{{ form.image.id_for_label }}" class="form-label">菜品图片</label>
                                        {{ form.image }}
                                    </div>
                                </div>
                            </div>
                            
                            <div class="mb-3">
                                <label for="{{ form.description.id_for_label }}" class="form-label">菜品描述</label>
                                {{ form.description }}
                            </div>
                            
                            <div class="mb-3">
                                <label for="{{ form.tags.id_for_label }}" class="form-label">相关标签</label>
                                {{ form.tags }}
                                <div class="form-text">可以选择多个标签</div>
                            </div>
                        </div>
                    </div>

                    <!-- 所需食材 -->
                    <div class="card mb-4">
                        <div class="card-header d-flex justify-content-between align-items-center">
                            <h5><i class="fas fa-shopping-basket me-2"></i>所需食材</h5>
                            <button type="button" class="btn btn-sm btn-outline-primary" onclick="addIngredient()">
                                <i class="fas fa-plus me-1"></i>添加食材
                            </button>
                        </div>
                        <div class="card-body">
                            {{ ingredient_formset.management_form }}
                            <div id="ingredient-forms">
                                {% for ingredient_form in ingredient_formset %}
                                <div class="ingredient-form row mb-3">
                                    <div class="col-md-4">
                                        {{ ingredient_form.name }}
                                        {{ ingredient_form.name.label_tag }}
                                    </div>
                                    <div class="col-md-2">
                                        {{ ingredient_form.amount }}
                                        {{ ingredient_form.amount.label_tag }}
                                    </div>
                                    <div class="col-md-2">
                                        {{ ingredient_form.unit }}
                                        {{ ingredient_form.unit.label_tag }}
                                    </div>
                                    <div class="col-md-3">
                                        {{ ingredient_form.notes }}
                                        {{ ingredient_form.notes.label_tag }}
                                    </div>
                                    <div class="col-md-1">
                                        <button type="button" class="btn btn-sm btn-outline-danger" onclick="removeIngredient(this)">
                                            <i class="fas fa-trash"></i>
                                        </button>
                                        {{ ingredient_form.DELETE }}
                                    </div>
                                </div>
                                {% endfor %}
                            </div>
                        </div>
                    </div>

                    <!-- 制作步骤 -->
                    <div class="card mb-4">
                        <div class="card-header d-flex justify-content-between align-items-center">
                            <h5><i class="fas fa-list-ol me-2"></i>制作步骤</h5>
                            <button type="button" class="btn btn-sm btn-outline-primary" onclick="addStep()">
                                <i class="fas fa-plus me-1"></i>添加步骤
                            </button>
                        </div>
                        <div class="card-body">
                            {{ step_formset.management_form }}
                            <div id="step-forms">
                                {% for step_form in step_formset %}
                                <div class="step-form mb-4 p-3 border rounded">
                                    <div class="row">
                                        <div class="col-md-2">
                                            {{ step_form.step_number }}
                                            {{ step_form.step_number.label_tag }}
                                        </div>
                                        <div class="col-md-10">
                                            {{ step_form.title }}
                                            {{ step_form.title.label_tag }}
                                        </div>
                                    </div>
                                    <div class="row mt-2">
                                        <div class="col-md-6">
                                            {{ step_form.cooking_time }}
                                            {{ step_form.cooking_time.label_tag }}
                                        </div>
                                        <div class="col-md-6">
                                            {{ step_form.heat_level }}
                                            {{ step_form.heat_level.label_tag }}
                                        </div>
                                    </div>
                                    <div class="mt-2">
                                        {{ step_form.description }}
                                        {{ step_form.description.label_tag }}
                                    </div>
                                    <div class="mt-2">
                                        {{ step_form.tips }}
                                        {{ step_form.tips.label_tag }}
                                    </div>
                                    <div class="mt-2">
                                        {{ step_form.image }}
                                        {{ step_form.image.label_tag }}
                                    </div>
                                    <div class="mt-2">
                                        <button type="button" class="btn btn-sm btn-outline-danger" onclick="removeStep(this)">
                                            <i class="fas fa-trash me-1"></i>删除步骤
                                        </button>
                                        {{ step_form.DELETE }}
                                    </div>
                                </div>
                                {% endfor %}
                            </div>
                        </div>
                    </div>

                    <div class="d-grid gap-2 d-md-flex justify-content-md-end">
                        <a href="{% if recipe %}{% url 'main:recipe_detail' recipe.id %}{% else %}{% url 'main:recipe_list' %}{% endif %}" class="btn btn-secondary me-md-2">取消</a>
                        <button type="submit" class="btn btn-primary">
                            {% if recipe %}更新做菜指南{% else %}创建做菜指南{% endif %}
                        </button>
                    </div>
                </form>
            </div>
        </div>
    </div>
</div>
{% endblock %}

{% block js %}
<script>
// 添加食材
function addIngredient() {
    const formCount = document.querySelector('#id_ingredients-TOTAL_FORMS').value;
    const container = document.getElementById('ingredient-forms');
    
    const template = `
        <div class="ingredient-form row mb-3">
            <div class="col-md-4">
                <input type="text" name="ingredients-${formCount}-name" class="form-control" placeholder="食材名称">
                <label class="form-label">食材名称</label>
            </div>
            <div class="col-md-2">
                <input type="text" name="ingredients-${formCount}-amount" class="form-control" placeholder="用量">
                <label class="form-label">用量</label>
            </div>
            <div class="col-md-2">
                <select name="ingredients-${formCount}-unit" class="form-select">
                    <option value="适量">适量</option>
                    <option value="g">克</option>
                    <option value="kg">千克</option>
                    <option value="ml">毫升</option>
                    <option value="个">个</option>
                    <option value="根">根</option>
                    <option value="片">片</option>
                    <option value="勺">勺</option>
                </select>
                <label class="form-label">单位</label>
            </div>
            <div class="col-md-3">
                <input type="text" name="ingredients-${formCount}-notes" class="form-control" placeholder="备注">
                <label class="form-label">备注</label>
            </div>
            <div class="col-md-1">
                <button type="button" class="btn btn-sm btn-outline-danger" onclick="removeIngredient(this)">
                    <i class="fas fa-trash"></i>
                </button>
                <input type="hidden" name="ingredients-${formCount}-DELETE">
            </div>
        </div>
    `;
    
    container.insertAdjacentHTML('beforeend', template);
    document.querySelector('#id_ingredients-TOTAL_FORMS').value = parseInt(formCount) + 1;
}

// 删除食材
function removeIngredient(button) {
    const form = button.closest('.ingredient-form');
    const deleteInput = form.querySelector('input[name$="-DELETE"]');
    deleteInput.value = 'on';
    form.style.display = 'none';
}

// 添加步骤
function addStep() {
    const formCount = document.querySelector('#id_steps-TOTAL_FORMS').value;
    const container = document.getElementById('step-forms');
    
    const template = `
        <div class="step-form mb-4 p-3 border rounded">
            <div class="row">
                <div class="col-md-2">
                    <input type="number" name="steps-${formCount}-step_number" class="form-control" min="1">
                    <label class="form-label">步骤序号</label>
                </div>
                <div class="col-md-10">
                    <input type="text" name="steps-${formCount}-title" class="form-control" placeholder="步骤标题">
                    <label class="form-label">步骤标题</label>
                </div>
            </div>
            <div class="row mt-2">
                <div class="col-md-6">
                    <input type="text" name="steps-${formCount}-cooking_time" class="form-control" placeholder="此步骤用时">
                    <label class="form-label">此步骤用时</label>
                </div>
                <div class="col-md-6">
                    <input type="text" name="steps-${formCount}-heat_level" class="form-control" placeholder="火候">
                    <label class="form-label">火候</label>
                </div>
            </div>
            <div class="mt-2">
                <textarea name="steps-${formCount}-description" class="form-control" rows="3" placeholder="详细描述制作过程..."></textarea>
                <label class="form-label">步骤描述</label>
            </div>
            <div class="mt-2">
                <textarea name="steps-${formCount}-tips" class="form-control" rows="2" placeholder="制作小贴士..."></textarea>
                <label class="form-label">小贴士</label>
            </div>
            <div class="mt-2">
                <input type="file" name="steps-${formCount}-image" class="form-control" accept="image/*">
                <label class="form-label">步骤图片</label>
            </div>
            <div class="mt-2">
                <button type="button" class="btn btn-sm btn-outline-danger" onclick="removeStep(this)">
                    <i class="fas fa-trash me-1"></i>删除步骤
                </button>
                <input type="hidden" name="steps-${formCount}-DELETE">
            </div>
        </div>
    `;
    
    container.insertAdjacentHTML('beforeend', template);
    document.querySelector('#id_steps-TOTAL_FORMS').value = parseInt(formCount) + 1;
}

// 删除步骤
function removeStep(button) {
    const form = button.closest('.step-form');
    const deleteInput = form.querySelector('input[name$="-DELETE"]');
    deleteInput.value = 'on';
    form.style.display = 'none';
}
</script>
{% endblock %}

